package edu.yctc.origin.controller.rest.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
 * 图书管理rest实现类
 * 
 * @author 赵宜珺
 *
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.origin.constant.ResultCode;
import edu.yctc.origin.controller.rest.ManageRestController;
import edu.yctc.origin.entity.BookDO;
import edu.yctc.origin.entity.ResultDO;
import edu.yctc.origin.service.ManageIService;
import edu.yctc.origin.vo.BookVO;

@RestController
@ComponentScan({"edu.yctc.origin.service"})
@RequestMapping("/restmanage")
public class ManageRestControllerImpl implements ManageRestController {

    @Autowired
    private ManageIService manageService;

    @Override
    @PostMapping("insert-book")
    public ResultDO<Void> insertBook(@RequestBody BookVO bookVO) {
        if (StringUtils.isBlank(bookVO.getName()) || StringUtils.isBlank(bookVO.getNumber())
            || StringUtils.isBlank(bookVO.getAuthor()) || StringUtils.isBlank(bookVO.getPress())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return manageService.insertBook(bookVO);
    }

    @Override
    @PostMapping("modify-book")
    public ResultDO<Void> modifyBook(@RequestBody BookVO bookVO) {
        if (StringUtils.isBlank(bookVO.getName()) || StringUtils.isBlank(bookVO.getNumber())
            || StringUtils.isBlank(bookVO.getAuthor()) || StringUtils.isBlank(bookVO.getPress()) || bookVO.getId() <= 0
            || bookVO.getStock() < 0) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return manageService.modifyBook(bookVO);
    }

    @Override
    @PostMapping("lend-book")
    public ResultDO<Void> lendBook(@RequestBody long id) {
        if (id <= 0) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return manageService.lendBook(id);
    }

    @Override
    @PostMapping("get-bookvos-by-name")
    public ResultDO<List<BookVO>> getBookVOsByName(@RequestBody String name) {
        if (StringUtils.isBlank(name)) {
            return new ResultDO<List<BookVO>>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        ResultDO<List<BookDO>> resultDO = manageService.getBookDOsByName(name);
        if (resultDO.isSuccess() == false) {
            return new ResultDO<List<BookVO>>(false, resultDO.getCode(), resultDO.getMsg(), null);
        }
        // 构建VO
        List<BookDO> bookDOs = resultDO.getModule();
        List<BookVO> bookVOs = new ArrayList<>();
        for (BookDO bookDO : bookDOs) {
            BookVO bookVO = manageService.getBookVOByBookDO(bookDO).getModule();
            if (bookVO == null) {
                continue;
            }
            bookVOs.add(bookVO);
        }
        return new ResultDO<List<BookVO>>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, bookVOs);
    }

}
